package com.cx.atguigu.controller;

import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisParam;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
import dev.langchain4j.community.model.dashscope.WanxImageModel;
import dev.langchain4j.data.image.Image;
import dev.langchain4j.data.message.ImageContent;
import dev.langchain4j.data.message.TextContent;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.output.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Base64;

/**
 * Description:
 *
 * @Author atguigu
 * @Create 2025/8/26 20:35
 * @Version 1.0
 */
@RestController
@Slf4j
public class ImageModelController {

    @Autowired
    private ChatModel chatModel;

    @Autowired
    private WanxImageModel wanxImageModel;

    @Value("classpath:static/target.jpg")
    private Resource resource;//import org.springframework.core.io.Resource

    //http://localhost:9006/image/call
    @GetMapping(value = "/image/call")
    public String readImageContent() throws IOException
    {

        byte[] byteArray = resource.getContentAsByteArray();
        String base64Data = Base64.getEncoder().encodeToString(byteArray);

        UserMessage userMessage = UserMessage.from(
                TextContent.from("从以下图片中提取文字"),
                ImageContent.from(base64Data,"image/jpg")
        );

        ChatResponse chatResponse = chatModel.chat(userMessage);
        String result = chatResponse.aiMessage().text();

        System.out.println(result);

        return result;
    }


    // http://localhost:9006/image/create2
    @GetMapping(value = "/image/create2")
    public String createImageContent2() throws IOException
    {
        System.out.println(wanxImageModel);
        Response<Image> imageResponse = wanxImageModel.generate("美女");

        System.out.println(imageResponse.content().url());

        return imageResponse.content().url().toString();

    }

    // http://localhost:9006/image/create3
    @GetMapping(value = "/image/create3")
    public String createImageContent3() throws IOException
    {
        String prompt = "18岁的中国美女，穿马面裙,甜妹,站在大草原上";
        ImageSynthesisParam param =
                ImageSynthesisParam.builder()
                        .apiKey(System.getenv("aliQwen-api"))
                        .model(ImageSynthesis.Models.WANX_V1)
                        .prompt(prompt)
                        .n(1)
                        .size("1024*1024")
                        .build();

        ImageSynthesis imageSynthesis = new ImageSynthesis();
        ImageSynthesisResult result = null;
        try {
            System.out.println("---sync call, please wait a moment----");
            result = imageSynthesis.call(param);
        } catch (ApiException | NoApiKeyException e){
            throw new RuntimeException(e.getMessage());
        }


        System.out.println(JsonUtils.toJson(result));
        return JsonUtils.toJson(result);
    }




}
